
<template>
  <div class="app-container">
    <!--添加-->
    <el-dialog title="添加数据" :visible.sync="open">
      <el-form :model="formroom">
        <el-form-item label="房源图片" :label-width="formLabelWidth">
          <el-upload :on-success="handleAvatarSuccess" :file-list="formroom.front_cover" :action="field104Action"
                     :before-upload="field105BeforeUpload">
            <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
          </el-upload>
          <el-button @click="kkyulan">文件预览</el-button>
        </el-form-item>

        <el-form-item label="房源名称" :label-width="formLabelWidth">
          <el-input v-model="formroom.room_name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="楼盘名称" :label-width="formLabelWidth">
          <el-input v-model="louname" placeholder="请输入楼盘名称" @blur="lou"/>
        </el-form-item>

        <el-form-item label="房源编号" :label-width="formLabelWidth">
          <el-input v-model="formroom.room_code" placeholder="请输入房源编号" />
        </el-form-item>

        <el-form-item label="面积" :label-width="formLabelWidth">
          <el-input v-model="formroom.proportion" placeholder="请输入面积" />
        </el-form-item>

        <el-form-item label="租金" :label-width="formLabelWidth">
          <el-input v-model="formroom.rental" placeholder="请输入租金" />
        </el-form-item>

        <el-form-item label="出租状态" :label-width="formLabelWidth">
            <el-radio v-model="formroom.status" label="1">已出租</el-radio>
            <el-radio v-model="formroom.status" label="0">未出租</el-radio>
        </el-form-item>


        <el-form-item label="发布人" :label-width="formLabelWidth">
          <el-select v-model="formroom.contacts" placeholder="请选择设置管家" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in field112Options" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="发布状态" :label-width="formLabelWidth">
            <el-radio v-model="formroom.st" label="1">已发布</el-radio>
            <el-radio v-model="formroom.st" label="0">未发布</el-radio>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="subadd">确 定</el-button>
      </div>
    </el-dialog>




    <!--查询-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房源名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="房源名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--列表-->
    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />

      <el-table-column label="图片">
        <template scope="scope">
          <el-image :src="scope.row.front_cover" style="width: 100px"></el-image>
        </template>
      </el-table-column>

      <el-table-column label="房源名称" align="center">
        <template scope="scope">
          <a v-html="scope.row.room_name"></a>
        </template>

      </el-table-column>


      <el-table-column label="楼盘名称" align="center" prop="name" />
      <el-table-column label="房源编号" align="center" prop="room_code" />
      <el-table-column label="面积" align="center" prop="proportion" />

      <el-table-column label="租金" align="center" prop="rental" />
      <el-table-column label="出租状态" align="center" prop="status">
        <template scope="scope">
          <span>{{ scope.row.status == 1 ? "已出租" : "未出租" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="contacts" />
      <el-table-column label="发布状态" align="center" prop="st">
        <template scope="scope">
          <span>{{ scope.row.st == 1 ? "已发布" : "未发布" }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listInfo, getInfo, delInfo, addInfo, updateInfo, selHousing,addroominfo} from "@/api/housings/xinxi/info";
import {Base64} from "js-base64";
import {listUser, selLou} from "@/api/housings/add/addfen";

export default {
  name: "Info",
  components: {
  },
  data() {
    return {
      field112Options:[],
      louname:'',
      formroom:{
        status:'',
        st:'',
        front_cover:'',
        room_name:'',
        room_code:'',
        proportion:'',
        rental:'',
        contacts:'',
        //楼盘名字
        name:'',
        louid:'',
      },
      field104Action:'http://localhost:7002/priture/upload',
      formLabelWidth: '120px',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房源信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 0,
        pageSize: 10,
        roomName: '',
        // buildingName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getuser()
  },
  methods: {
    subadd(){
      console.log(this.formroom)
      addroominfo(this.formroom).then(res=>{
        if(res){
          alert("添加成功")
          this.getList()
        }else{
          alert("添加失败")
        }
        this.open=false

      })
    },
    /*获取管家*/
    getuser(){
      listUser(this.queryParams).then(response => {
        console.log(response.rows)
        this.field112Options = response.rows;
      });
    },
    /*添加查询楼盘是否存在*/
    lou(){
      console.log(this.louname)
      if(this.louname==null || this.louname==""){
        return false;
      }
      selLou(this.louname).then(res=>{
        console.log(res.ss)
        if(res.ss==200){
          alert("楼盘查找成功")
          this.formroom.name=this.louname
          this.formroom.louid=res.msg
        }else{
          alert("没有此楼盘")
        }
      })
    },
    //文件预览
    kkyulan(){
      var url=this.formroom.front_cover
      window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url)));
    },
    /** 上传图片返回数据*/
    handleAvatarSuccess(res,file){
      this.formroom.front_cover=res.url
    },
    /** 图片验证规则 */
    field105BeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    /** 查询房源信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        console.log(response.rows)
        this.infoList = response.rows;
        this.total = response.rows[0].total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        houseId: null,
        frontCover: null,
        roomName: null,
        roomCode: null,
        proportion: null,
        direction: null,
        rental: null,
        decorateStyle: null,
        buildingName: null,
        floor: null,
        floorCount: null,
        status: 0,
        privateFacility: null,
        recommended: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        deptId: null,
        createTime: null,
        styleId: null,
        finishDate: null,
        vacancyDate: null,
        delFlg: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 0;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房源信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房源信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除房源信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `system_info.xlsx`)
    }
  }
};
</script>
